package com.itqn.service.Impl;

import com.itqn.dao.EmployeeDao;
import com.itqn.dao.Impl.EmployeeDaoImpl;
import com.itqn.pojo.Employee;
import com.itqn.pojo.PageBean;
import com.itqn.service.EmployeeService;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * 员工表业务逻辑层接口的实现类
 */
public class EmployeeServiceImpl implements EmployeeService {
    private EmployeeDao employeeDao=new EmployeeDaoImpl();

    @Override
    public Employee login(Employee employee) throws Exception {
        // 对密码进行MD5加密,匹配数据库中已经加密过的值
        employee.setPassword(DigestUtils.md5Hex(employee.getPassword()));
        return employeeDao.login(employee);
    }

    @Override
    public int UpPassword(Employee employee, String oldPassword) throws Exception {
        Employee employeeById = employeeDao.findEmployeeById(Integer.parseInt(employee.getId().toString()));
        if (!DigestUtils.md5Hex(oldPassword).equals(employeeById.getPassword())) {
            return 3;
        }
        if (oldPassword.equals(employee.getPassword())) {
            return 2;
        }
        employee.setPassword(DigestUtils.md5Hex(employee.getPassword()));
        return employeeDao.UpPassword(employee);
    }

    @Override
    public int AddEmployee(Employee employee) throws Exception {
        // 设置密码默认为123456,并对其进行MD5加密存储
        employee.setPassword(DigestUtils.md5Hex("123456"));
        return employeeDao.AddEmployee(employee);
    }

    @Override
    public Employee findEmployeeById(int id) throws Exception {
        return employeeDao.findEmployeeById(id);
    }

    @Override
    public int updateEmployee(Employee employee) throws Exception {
        return employeeDao.updateEmployee(employee);
    }

    @Override
    public Integer disable(Employee employee) throws Exception {
        return employeeDao.disable(employee);
    }

    @Override
    public PageBean<Employee> query(String name, Integer pageSize, Integer page) throws Exception {
        PageBean pb = new PageBean();
        // 封装商品集合
        pb.setList(employeeDao.query(name, pageSize, page));
        // 封装总条数
        Integer totalCount = employeeDao.totalCount(name, pageSize, page);
        pb.setTotalCount(totalCount);
        // 封装总页数
        pb.setPageCount(totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize + 1));
        return pb;
    }

    @Override
    public void updatestatic(Employee employee) throws Exception {
        employeeDao.updatestatic(employee);
    }
}
